﻿namespace PugLib.Extensions.Collections
{
	using System;
	using System.Collections.Generic;

	public static class SortingExtensionProvider
	{
		public static void Sort<T>(this IList<T> list) where T : IComparable
		{
			Sort(list, 0, list.Count - 1);
		}

		public static void Sort<T>(this IList<T> list, int startIndex, int endIndex) where T : IComparable
		{
			//Bubble Sort
			for (int i = startIndex; i < endIndex; i++)
			{
				for (int j = endIndex; j > i; j--)
				{
					if (list[j].IsLessThan(list[j - 1]))
					{
						list.Swap(j, j - 1);
					}
				}
			}
		}

		private static void Swap<T>(this IList<T> list, int index1, int index2)
		{
			T tmp = list[index1];
			list[index1] = list[index2];
			list[index2] = tmp;
		}

		private static bool IsLessThan(this IComparable value, IComparable item)
		{
			return value.CompareTo(item) < 0;
		}
	}
}